package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Resource
    private CustomerDao customerDao;
    @Override
    public Map<String, Object> list(Integer page, Integer rows, String customerName) {
        page = page==0?1:page;
        int offSet = (page - 1) * rows;
        List<Customer> customers = customerDao.getCustomerList(offSet, rows, customerName);
        Map<String, Object> map = new HashMap<>();
        map.put("total", customerDao.getCustomerCount(customerName));
        map.put("rows", customers);
        return map;
    }

    @Override
    public ServiceVO save(Customer customer) {
        if (customer.getCustomerId() == null){
            //添加
            customerDao.insert(customer);
        }else {
            //修改
            customerDao.updateById(customer);
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO delete(String ids) {
        String[] idArray = ids.split(",");
        for (String id : idArray) {
            customerDao.delete(id);
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);

    }
}
